﻿var dx = 'tbody tr td .dxeEditArea';
var _freeDays = parseInt($('#ASPxFreeDays').find(dx).val()),
    _maxDays = parseInt($("#ASPxMaxDays").find(dx).val()),
    _minPriceWithDiscount = parseInt($("#ASPxMinPriceWithDiscount").find(dx).val()),
    _oneDayPlacementPrice = parseInt($("#ASPxOneDayPlacementPrice").find(dx).val()),
     now = new Date(),
    d = new Date(now.getFullYear(), 0, 1, 0, 0, 0),
    _dayOfYear = Math.floor((now - d) / 86400000),
    //todo:??
    _minPriceTerm = parseInt($("#ASPxMinPriceForRaiseWithout").find(dx).val()),
    _maxDiscountTerm = parseInt($('#ASPxMaxDiscount').find(dx).val()),
    
     dateSlider = $('#date-slider'),
    priceSlider = $('#price-slider'),
    
        dateFrom = $('#datefrom tbody tr td .dxeEditArea'),
    dateTo = $('#dateto tbody tr td .dxeEditArea'),

    txtFreeDays = $('#ASPxFreeDays');
txtDatefrom = $("#ASPxDatefrom"),
txtDateTo = $("#ASPxDateTo"),
daysRange = $('#daysRange'),
priceAfterDiscount = $('#priceAfterDiscount'),
priceBeforeDiscount = $('#priceBeforeDiscount');

$(document).ready(function () {
    debugger;
    setDefaults();
});

function setDefaults() {
    var _dateFrom = dateFromString(dateFrom.val());
    var _dateTo = dateFromString(dateTo.val());
    
    var range = getDateRange(_dateFrom, _dateTo);

    dateSlider.slider("option", "values", [_freeDays + _dateFrom.getDayOfYear(), _freeDays + _dateFrom.getDayOfYear() + range]);

    priceSlider.slider("option", "values", [parseFloat(priceAfterDiscount.text()), parseFloat(priceBeforeDiscount.text())]);
    
    //var dr = range + _freeDays;
    //liftMessageSlider.slider("option", "max", dr);
    //liftMessageCount.text(dr);
    //priceSlider2.slider("option", "max", dr);
}

//----------------EVENTS-----------------------------

dateFrom.on('change', function () {
    var _dateFrom = dateFrom.val();
    var _dateTo = dateTo.val();
    var _discountTerm = discountTerm.text();
    
    if ((new Date(_dateFrom)).getTime() < (new Date())) {
        _dateFrom = GetDateDefaultValue();
    }
    
    var range = getDateRange(_dateFrom, _dateTo);

    if (range <= _freeDays) {
        _dateFrom = GetDateDefaultValue();
        dateSlider.slider("option", "values", [_dayOfYear + _freeDays, _dayOfYear + _freeDays]);
    }
    else {
        dateSlider.slider("option", "values", [_dayOfYear + _freeDays, _dayOfYear + _freeDays + range]);
    }
    
    CalcAndSetPriceTerm(_dateFrom, _dateTo, _discountTerm, _oneDayPlacementPrice);
});

dateTo.on('change', function () {
    var _dateFrom = dateFrom.val();
    var _dateTo = dateTo.val();
    var _discountTerm = discountTerm.text();
    
    var range = getDateRange(_dateTo, new Date());
    
    var values = dateSlider.slider("option", "values");
    
    if (range > _maxDays) {
        var cd = new Date();
        cd.setDate(cd.getDate() + _maxDays);
        var fd = ((cd.getMonth() + 1) + '/' + cd.getDate() + '/' + cd.getFullYear()).toString();
        //todo
        dateTo.val(fd);
        var range2 = getDateRange(_dateFrom, _dateTo);
        dateSlider.slider("option", "values", [values[0], _dayOfYear + range2 + _freeDays]);
    } else {
        dateSlider.slider("option", "values", [values[0], _dayOfYear + range]);
    }
    
    CalcAndSetPriceTerm(_dateFrom, _dateTo, _discountTerm, _oneDayPlacementPrice);
});

// gets the default date value
function GetDateDefaultValue() {
    var cd = new Date(),
    fd = (cd.getMonth() + 1).toString() + '/' + cd.getDate().toString() + '/' + cd.getFullYear().toString();
    return fd;
}


// -------date slider------
dateSlider.slider({ values: [10, 25] });
dateSlider.slider("option", "min", _dayOfYear);
dateSlider.slider("option", "max", _dayOfYear + _maxDays);

//--------price slider-----
priceSlider.slider({ values: [_minPriceTerm, _minPriceTerm + 1000] });
priceSlider.slider("option", "min", 0);
priceSlider.slider("option", "max", _oneDayPlacementPrice * _maxDays);


//1.1
dateSlider.slider({
    slide: function (event, ui) {
        var _dateFrom = dateFrom.val();
        var _dateTo = dateTo.val();
        var _discountTerm = discountTerm.text();

        var fstPos = _freeDays + _dayOfYear;
        var values = ui.values;
        if (values[0] != fstPos) {
            return false;
        }
        if (values[1] <= fstPos) {
            return false;
        }
        
        var dtstr = changeDateRange(values);
        dateTo.val(dtstr);
        
        CalcAndSetPriceTerm(_dateFrom, _dateTo, _discountTerm, _oneDayPlacementPrice);

        var dr = getDateRange(_dateFrom, _dateTo) + _freeDays;
        discountTerm.text(parseInt(((dr - _freeDays) * _maxDiscountTerm) / (_maxDays - _freeDays)).toString());
        liftMessageSlider.slider("option", "max", dr);
        liftMessageCountSlider.slider("option", "max", dr);
        liftMessageCountSlider.slider("option", "value", dr);
        liftMessageCount.text(dr);
        calcPriceForLifts(null, liftMessageSlider.slider("option", "value"));
        priceSlider2.slider("option", "max", dr);
    }
});

//1.2
priceSlider.slider({
    slide: function (event, ui) {
        return false;
    }
});

function changeDateRange(values) {
    var dateFrom = new Date(),
    sy = dateFrom.getFullYear();
    dateFrom = new Date(sy, 0, 1);
    dateFrom.setDate(dateFrom.getDate() + values[0]);
    var dateTo = new Date();
    dateTo.setDate(dateFrom.getDate() + Math.ceil(values[1] - values[0] + _freeDays));
    var dtstr = (dateTo.getMonth() + 1).toString() + '/' + (dateTo.getDate().toString()) + '/' + dateTo.getFullYear().toString();
    return dtstr;
}

//insert prices to 1.2 and moving slider
function CalcAndSetPriceTerm(dateFrom, dateTo, discountTerm, oneDayPlacementPrice) {
    var p = price(dateFrom, dateTo, oneDayPlacementPrice);
    var pad = parseInt(p - (p * parseInt(discountTerm) / 100));

    priceBeforeDiscount.text(p);
    priceAfterDiscount.text(pad);
    
    priceSlider.slider("option", "values", [p, pad]);
}

//returns price for chosen period
function price(dateFrom, dateTo, oneDayPlacementPrice) {
    var daysCount = getDateRange(dateFrom, dateTo);
    var price = daysCount * oneDayPlacementPrice;
    return price;
}

function dateFromString(str) {
    var from = str.split("-");
    return new Date(from[0], from[1] - 1, from[2]);
}

//returns pay days (count of days from start date to end date minus free days)
function getDateRange(dateFrom, dateTo) {
    var oneDay = 24 * 60 * 60 * 1000; // hours*minutes*seconds*milliseconds
    return Math.round(Math.abs((dateFrom.getTime() - dateTo.getTime()) / (oneDay)));
}

Date.prototype.getDayOfYear = function () {
    var onejan = new Date(this.getFullYear(), 0, 1);
    return Math.ceil((this - onejan) / 86400000);
}